/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package application.tools;

/**
 *
 * @author ajdin
 *
 */
public class ConflationTool {

    public boolean howManyMatchingChars(String source, String target) {
        //source = "Autistic";  //i.e. search term
        //target = "Autism";  //from Disco/WordN.
        //source = "Simulations";  //i.e. search term
        //target = "simulation";  //from Disco/WordN.
        /*
         * ALGORITHM:
Step 1: If the source is a substring of the target, then True
        Else
Step 2: Calculate number of matches of the target in the source/length of the target (Match Ratio)
Step 3: If the target word length is longer or equal to the source length
Then
Step 4:   If Match Ratio greater than or equal to 75% then True
Else (Target word length shorter than source length)
Step 5: If Match Ratio greater than 1 – (target length/[4*Source Length]) then True
         */

        int numOfMatches = 0; // char match counter
        int matchRatio = 0;
        boolean acceptWord = false;

        source = source.toLowerCase();
        target = target.toLowerCase();

        if(target.contains(source)) { //Step 1:
            acceptWord = true;
        } else if (target.length() < 6) {
            acceptWord = false;
        } else {
        //else {

            char[] targetArray = target.toCharArray(); //convert source string to array of chars

            //loop though the source chars & count matching chars
            for (int p = 0; p < targetArray.length; p++) {      //Step 2:
                if (source.contains(Character.toString(targetArray[p]))) {
                    numOfMatches++;
                }
            }

            //System.out.println( this.getClass().getName() +" -> "+numOfMatches + " out of "+target.length());

            //get the matching ratio between the two input strings
            //double matchPct = target.length() * 0.66;

            /*
            //Success percentage = 2/3 + (1- length(target)/length(source))*1/3
            double matchPct2thirds = target.length() * 0.66;
            double matchPct1third = target.length() * 0.33;
            double successPercentage = (matchPct2thirds + (1 - target.length()/source.length()) * matchPct1third * 10);
             *
             */

            double successPercentage = ((double)numOfMatches / ((double)target.length()) * 100);

            //System.out.println("successPercentage is "+successPercentage);
            
            if(target.length()>=source.length()) { //Step 3
                if(successPercentage >= 75) { //Step 4:
                    acceptWord = true;
                }
            } else {
                //double tmpSuccessPercentage = 0.00;
                double tmpSuccessPercentage = (1 - (((double) target.length()) / (4 * ((double) source.length())))) * 100;
                //float tmpSuccessPercentage = (1 - (20 / (4 * source.length()))) * 100;
                //System.out.println("tmpSuccessPercentage is "+tmpSuccessPercentage);
                if(successPercentage > tmpSuccessPercentage) {
                    acceptWord = true;
                }
            }
            //wrong
            /*
            if (numOfMatches >= successPercentage) {
                acceptWord = true;
            }*/
            //wrong
           

        }

        return acceptWord;
    }

}
